package com.ww.editsms.widget.wheel;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

import com.chinawangw.R;
import com.ww.editsms.widget.wheel.widget.ArrayWheelAdapter;
import com.ww.editsms.widget.wheel.widget.OnWheelChangedListener;
import com.ww.editsms.widget.wheel.widget.WheelView;


public class WheelCascade {

	private Context context;
	private Dialog dlg;

	private WheelView wheel1, wheel2;
	private Button sure, cancel;

	public WheelCascade(Context context) {
		this.context = context;
	}

	/**
	 * 公共对话框.
	 * @param title 标题
	 * @param content 内容
	 * @param okClick 确定按钮点击事件.
	 * @return AlertDialog
	 */
	public void show(String[] data1, String data2[][], int check1, int check2) {
		dlg = new Dialog(context, R.style.MyDialog);
		dlg.show();
		Window window = dlg.getWindow();
		window.setContentView(R.layout.core_wheel_2);
		findView(window);
		initiWheelView(data1, data2, check1, check2);
		sure.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				cancel();// 此处一定要调用finish()方法
			}
		});
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				cancel();// 此处一定要调用finish()方法
			}
		});
	}
	
	
	/**
	 * 公共对话框.
	 * @param title 标题
	 * @param content 内容
	 * @param okClick 确定按钮点击事件.
	 * @return AlertDialog
	 */
	public void show(String[] data1, String data2[][], int check1, int check2,OnClickListener click) {
		dlg = new Dialog(context, R.style.MyDialog);
		dlg.show();
		Window window = dlg.getWindow();
		window.setContentView(R.layout.core_wheel_2);
		findView(window);
		initiWheelView(data1, data2, check1, check2);
		sure.setOnClickListener(click);
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				cancel();// 此处一定要调用finish()方法
			}
		});
	}

	private void findView(Window window) {
		wheel1 = (WheelView) window.findViewById(R.id.wheel2_1);
		wheel2 = (WheelView) window.findViewById(R.id.wheel2_2);
		sure = (Button) window.findViewById(R.id.buttonsure);
		cancel = (Button) window.findViewById(R.id.buttoncancle);
	}

	public int getResult() {
		return wheel2.getCurrentItem();
	}
	
	public int[] getResult2() {
		return new int[]{wheel1.getCurrentItem(),wheel2.getCurrentItem()};
	}

	private void initiWheelView(String[] data1, final String[][] data2, int check1, int check2) {
		wheel1.setVisibleItems(3);
		wheel1.setAdapter(new ArrayWheelAdapter<String>(data1));
		wheel2.setVisibleItems(3);
		wheel1.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				wheel2.setAdapter(new ArrayWheelAdapter<String>(data2[newValue]));
				wheel2.setCurrentItem(data2[newValue].length / 2);
			}
		});
		wheel1.setCurrentItem(check1);
	}

	public void cancel() {
		if (dlg != null) {
			dlg.cancel();
		}
	}
}
